﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Description;
using MuseumApp315.Data;
using MuseumApp315.Data.Models;

namespace MuseumApp315.Controllers
{
    public class MuseumsApiLunchController : ApiController
    {
        private ApplicationDbContext db = new ApplicationDbContext();

        // GET: api/MuseumsApiLunch
        public IHttpActionResult Get()
        {
            List<Museum> items = null;
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                items = db.Museums.Include(r => r.Reviews).ToList();
            }

            return Ok(items.Select(m => new
            {
                MuseumId = m.MuseumId,
                Name = m.Name,
                City = m.City,
                Description = m.Description,
                Reviews = m.Reviews.Select(r => new
                {
                    ReviewId = r.ReviewId,
                    UserName = r.UserName
                })
            }));
        }

        // GET: api/MuseumsApiLunch/5
        [ResponseType(typeof(Museum))]
        public IHttpActionResult GetMuseum(int id)
        {
            Museum museum = db.Museums.Find(id);
            if (museum == null)
            {
                return NotFound();
            }

            return Ok(museum);
        }

        // PUT: api/MuseumsApiLunch/5
        [ResponseType(typeof(void))]
        public IHttpActionResult PutMuseum(int id, Museum museum)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (id != museum.MuseumId)
            {
                return BadRequest();
            }

            db.Entry(museum).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!MuseumExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return StatusCode(HttpStatusCode.NoContent);
        }

        // POST: api/MuseumsApiLunch
        [ResponseType(typeof(Museum))]
        public IHttpActionResult PostMuseum(Museum museum)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            db.Museums.Add(museum);
            db.SaveChanges();

            return CreatedAtRoute("DefaultApi", new { id = museum.MuseumId }, museum);
        }

        // DELETE: api/MuseumsApiLunch/5
        [ResponseType(typeof(Museum))]
        public IHttpActionResult DeleteMuseum(int id)
        {
            Museum museum = db.Museums.Find(id);
            if (museum == null)
            {
                return NotFound();
            }

            db.Museums.Remove(museum);
            db.SaveChanges();

            return Ok(museum);
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }

        private bool MuseumExists(int id)
        {
            return db.Museums.Count(e => e.MuseumId == id) > 0;
        }
    }
}